package com.tianqu.tms.www.transfer.action;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import com.tianqu.tms.core.base.Logger;
import com.tianqu.tms.core.base.TmsAction;
import com.tianqu.tms.core.base.UserContext;
import com.tianqu.tms.core.util.StringUtil;
import com.tianqu.tms.core.util.TmsConstants;
import com.tianqu.tms.www.transfer.actionform.TransferRgActionForm;
import com.tianqu.tms.www.transfer.controller.CarStockAcceptListController;
import com.tianqu.tms.www.transfer.viewhelper.TransferRgInputViewHelper;

/**
 * 库存车辆受理-车辆一览 action
 * @author mufeiyu
 *
 */
public class CarStockAcceptListAction extends TmsAction {

	
	private Logger logger = Logger.getLogger(CarStockAcceptListAction.class);
	@Override
	protected ActionForward doAction(ActionMapping mapping, ActionForm form,
			HttpServletRequest request, HttpServletResponse response,
			UserContext uc) throws Exception {
		logger.debug(this.getClass().getName() + "#execute was called.");

		TransferRgActionForm transferForm = (TransferRgActionForm)form;
		String serial = "";
		String licensePlate = "";
		String vin = "";
		if(!StringUtil.isNullOrBlank((String)uc.getAttribute("carSerial"))){
			serial = (String)uc.getAttribute("carSerial");
		}else {
			serial = transferForm.getSerial();
		}
		if(!StringUtil.isNullOrBlank((String)uc.getAttribute("licensePlate"))){
			licensePlate = (String)uc.getAttribute("licensePlate");
		}else {
			licensePlate = transferForm.getLicensePlate();
		}
		if(!StringUtil.isNullOrBlank((String)uc.getAttribute("vin"))){
			vin = (String)uc.getAttribute("vin");
		}else {
			vin = transferForm.getVin();
		}
		if(StringUtil.isNullOrBlank(serial)
				&&StringUtil.isNullOrBlank(licensePlate)
				&&StringUtil.isNullOrBlank(vin)){
			setErrorsToRequest(request, TmsConstants.U0040, null); // 必须输入一个查询条件。
			return mapping.getInputForward();
		}
		if(!StringUtil.isNullOrBlank(serial)
				&&(!StringUtil.isNullOrBlank(licensePlate)
				||!StringUtil.isNullOrBlank(transferForm.getVin()))){
			setErrorsToRequest(request, TmsConstants.U0040, null); // 必须输入一个查询条件。
			return mapping.getInputForward();
		}
		if(StringUtil.isNullOrBlank(serial)
				&&(!StringUtil.isNullOrBlank(licensePlate)
				&&!StringUtil.isNullOrBlank(vin))){
			setErrorsToRequest(request, TmsConstants.U0040, null); // 必须输入一个查询条件。
			return mapping.getInputForward();
		}
		//车牌5位check
		if (!StringUtil.isNullOrBlank(licensePlate) && licensePlate.length() !=5) {
			setErrorsToRequest(request, TmsConstants.U8009, null); //请输入车牌号码后5位。
			return mapping.getInputForward();
		}
		//车架号6位check
		if (!StringUtil.isNullOrBlank(vin) && vin.length() !=6) {
			setErrorsToRequest(request, TmsConstants.U8010, null); //请输入车架号后6位。
			return mapping.getInputForward();
		}

		CarStockAcceptListController controller = new CarStockAcceptListController();
		controller.execute(uc);
		
		TransferRgInputViewHelper viewHelper = new TransferRgInputViewHelper();
		viewHelper = (TransferRgInputViewHelper)uc.getAttribute(TmsConstants.VIEWHELP);
		
		if ("true".equals((String)uc.getAttribute("emptyFlag"))){
			setErrorsToRequest(request, TmsConstants.U0039, null);// 查询结果不存在。
			return mapping.getInputForward();
		}
		//向viewhelp中放一个字符串
		viewHelper.setLcText((String)uc.getAttribute("lcText"));
		viewHelper.setCyText((String)uc.getAttribute("cyText"));
		viewHelper.setLcPrintBsFlag((String)uc.getAttribute("lcPrintFlag"));
		uc.removeAttribute("lcText");
		uc.removeAttribute("cyText");
		uc.removeAttribute("lcPrintFlag");
		
		request.setAttribute(TmsConstants.VIEWHELP, viewHelper);		
		logger.debug(this.getClass().getName() + "#execute was completed.");
		return mapping.findForward(TmsConstants.SUCCESS);
	}
}